package com.example.customer.controller;

import com.example.customer.entity.Contact;
import com.example.customer.entity.CusInfo;
import com.example.customer.entity.Customer;
import com.example.customer.service.CusInfoService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/")
public class CusInfoController {

    @Resource(name = "CusInfoServiceImpl")
    private CusInfoService cusInfoService;

    //查询全部信息
    @RequestMapping("/cusInfo")
    public String cusInfo(@ModelAttribute Customer customer ,
                          @RequestParam(value="pageNum",defaultValue="1")int pageNum,
                          @RequestParam(value="pageSize",defaultValue="5")int pageSize,
                          HttpServletRequest request){

        PageInfo<Customer> pageInfo = cusInfoService.queryCustomer(pageNum,pageSize);
        request.setAttribute("pageInfo",pageInfo);
        return "/customer/cusInfo/customerIndex";
    }

    // 条件查询
    @RequestMapping("/query_cus")
    public String query_cus(@ModelAttribute Customer customer ,
                            @RequestParam(value="pageNum",defaultValue="1")int pageNum,
                            @RequestParam(value="pageSize",defaultValue="5")int pageSize,
                            HttpServletRequest request){

        PageInfo<Customer> pageInfo = cusInfoService.queryCondition(customer,pageNum,pageSize);
        request.setAttribute("pageInfo",pageInfo);
        return "/customer/cusInfo/customerIndex";
    }

    // 根据id删除
    @RequestMapping("/del_cus")
    public String del_cus(String id ,  @RequestParam(value="pageNum",defaultValue="1")int pageNum,
                          HttpServletRequest request){

        // int num = Integer.parseInt(request.getParameter("pageNum")) ;
         String ID = request.getParameter("id");
         cusInfoService.delInfo(ID);
        return "redirect:cusInfo?pageNum="+pageNum;
    }


    //修改customer/cusInfo
    @RequestMapping("/upd_customer")
    public String upd_customer(Customer customer , String type, CusInfo cusInfo,
                               @RequestParam(value="pageNum",defaultValue="1")int pageNum,
                               @RequestParam(value="pageSize",defaultValue="5")int pageSize,
                               HttpServletRequest request){

        String id = customer.getCus_id();
        String result = "";
        if("query".equals(type)){
            List<Map> map = cusInfoService.queryInfo(id);
            request.setAttribute("map",map);
            result = "/customer/cusInfo/customerUpdate";
        }else if("modify".equals(type)){
            CusInfo info = cusInfoService.queryCusInfo(cusInfo);
            if(null!=info){
                cusInfoService.updCusInfo(cusInfo);
                cusInfoService.updCustomer(customer);
            }else{  // CusInfo 不存在则新增
                cusInfoService.saveCusInfo(cusInfo);
                cusInfoService.updCustomer(customer);
            }

            result = "redirect:cusInfo?pageNum="+pageNum;
        }

        return result;
    }


    @RequestMapping("/cusLost")
    public String cusLost(@ModelAttribute Customer customer ,
                          @RequestParam(value="pageNum",defaultValue="1")int pageNum,
                          @RequestParam(value="pageSize",defaultValue="5")int pageSize,
                          HttpServletRequest request){

        PageInfo<Customer> pageInfo = cusInfoService.queryLost(customer,pageNum,pageSize);
        request.setAttribute("pageInfo",pageInfo);
        return "/customer/cusLost/lost";
    }

    //修改客户流失状态
    @RequestMapping("/upd_lost")
    public String upd_lost(@ModelAttribute Customer customer ,  String type,
                          @RequestParam(value="pageNum",defaultValue="1")int pageNum,
                          @RequestParam(value="pageSize",defaultValue="5")int pageSize,
                          HttpServletRequest request){

        String id = customer.getId();

        if("confirm".equals(type)){
            cusInfoService.updStatus(id,"确认流失");
        }else if("delay".equals(type)){
            cusInfoService.updStatus(id,"暂缓流失");
        }

        return  "redirect:cusLost?pageNum="+pageNum;
    }



    //查看联系人
    @RequestMapping("/show_contact")
    public String show_contact(String cus_id ,
                          @RequestParam(value="pageNum",defaultValue="1")int pageNum,
                          @RequestParam(value="pageSize",defaultValue="5")int pageSize,
                          HttpServletRequest request){

        String id = request.getParameter("cus_id");
        PageInfo<Contact> pageInfo = cusInfoService.queryByCID(id,pageNum,pageSize);
        request.setAttribute("pageInfo",pageInfo);
        return "/customer/cusInfo/linkMan";
    }

    //删除一条联系人
    @RequestMapping("/del_contact")
    public String del_contact(String id ,String cus_id,
                               @RequestParam(value="pageNum",defaultValue="1")int pageNum,
                               @RequestParam(value="pageSize",defaultValue="5")int pageSize,
                               HttpServletRequest request){

        int num = Integer.parseInt(request.getParameter("pageNum")) ;
        String ID = request.getParameter("id");
        String cid = request.getParameter("cus_id");
        cusInfoService.delContact(ID);
        PageInfo<Contact> pageInfo = cusInfoService.queryByCID(cid,pageNum,pageSize);
        request.setAttribute("pageInfo",pageInfo);
        return "/customer/cusInfo/linkMan";
    }

    //修改联系人信息
    @RequestMapping("/upd_contact")
    public String upd_contact(@ModelAttribute Contact contact ,String type,
                              @RequestParam(value="pageNum",defaultValue="1")int pageNum,
                              @RequestParam(value="pageSize",defaultValue="5")int pageSize,
                              HttpServletRequest request){

        String id = contact.getId();
        String cmd = request.getParameter("type");

        String result = "";
        if("query".equals(cmd)){
            Contact data = cusInfoService.queryById(id);
            request.setAttribute("Contact",data);
            result = "/customer/cusInfo/linkManUpdate";
        }else if("modify".equals(cmd)){
            Contact data = cusInfoService.queryById(id);

            String cid = data.getCus_id();
            cusInfoService.modify(contact);
            result = "redirect:show_contact?cus_id="+cid+"&pageNum="+pageNum;
        }

        return result;
    }
}
